<?php

namespace App\Exports;

use App\Goods;
use App\GroupBuyItem;
use App\Order;
use App\OrderDetail;
use App\Sku;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;

class OrderExport extends StringValueBinder implements FromCollection,WithCustomValueBinder,WithHeadings,ShouldAutoSize
{
    public $start_time;
    public $end_time;
    public $status;
    public function __construct($start_time,$end_time,$status)
    {
        $this->start_time=$start_time;
        $this->end_time=$end_time;
        $this->status=$status;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $arr=collect([]);
        $status=$this->status;
        $start_time=$this->start_time;
        $end_time=$this->end_time;
        $result=Order::with(['user','share'])->when($this->status,function ($query) use ($status) {
            switch ($status) {
                case 0:
                    return $query;
                    break;
                case 1:
                    return $query->where('status', -1);
                    break;
                case 2:
                    return $query->where([['status', '=', 0]]);
                    break;
                case 3:
                    return $query->where([['status', '=', 1]])->orWhere('status','=',2);
                    break;
                case 4:
                    return $query->where([['status','=',1],['deliveryStatus','=',0]]);
                    break;
                case 5:
                    return $query->where([['status','=',1],['deliveryStatus','=',1],['receivingStatus','=',0]])->orWhere([['status','=',2],['deliveryStatus','=',1],['receivingStatus','=',0]]);
                    break;
                case 6:
                    return $query->where([['status','=',1],['deliveryStatus','=',1],['receivingStatus','=',1]])->orWhere([['status','=',2],['deliveryStatus','=',1],['receivingStatus','=',1]]);
                    break;
//                case 4:
//                    return $query->where([['status','=',-1]]);
//                    break;
            }
        })->when($this->start_time,function ($query)use ($start_time){
            $query->where('created_at','>=',$start_time);
        })->when($end_time,function ($query)use ($end_time){
            $query->where('created_at','<=',$this->end_time);
        })->latest()->get()->toArray();
        foreach ($result as $key=>$value){
            $detail=OrderDetail::where('order_id',$value['order_number'])->get();
            $names="";
            if ($value['is_group_buy']){
                if ($value['group_item_id']){
                    $groupItem=GroupBuyItem::find($value['group_item_id']);
                    $group=GroupBuyItem::with('user')->where('group_master_id',$groupItem['group_master_id'])->get();
                    if ($group){
                        $names=$group->pluck("user.nickname")->implode(",");
                    }
                }
            }
            $group_status="";
            if ($value['is_group_buy']){
                switch ($value['group_status']){
                    case 0:
                        $group_status="拼团中";
                        break;
                    case 1:
                        $group_status="是";
                        break;
                    case -1:
                        $group_status="否";
                        break;
                }
            }

            $money=0;
            foreach ($detail as $k=>$v){
                $data=[
                    "uid"=>$value["uid"],
                    "nickname"=>isset($value["user"])?$value["user"]["nickname"]:"",
                    "order_number"=>$v['order_id'],
                    "merchant_number"=>$value["merchant_number"],
                    "refund_number"=>$value["refund_number"],
                    "goods_name"=>$v["goods_name"],
                    "sku"=>$v["sku"],
                    "number"=>$v["number"],
                    "origin_price"=>$v['origin_price'],
                    "pay_detail_price"=>$v['price']*$v['number'],
                    "created_at"=>$value["created_at"],
                    "pay_time"=>$value["pay_time"],
                    "is_group"=>$value["is_group_buy"]?"是":"否",
                    "express_name"=>$value["delivery_name"],
                    "tracking_no"=>$value["tracking_no"],
                    "pay_status"=>$value["status_text"],
                    "delivery_status"=>$value["delivery_status_text"],
                    "receive_status"=>$value["receive_status_text"],
                    "address_info"=>$value["address"],
                    "group_user"=>$names,
                    "group_success"=>$group_status,
                    "share"=>isset($value['share'])?$value['share']['nickname']:'',
                    "type"=>$v["buy_type"]==1?"赠送":"正常购买"


                ];
                $arr->push($data);
            }
        }
        return $arr;
    }
    public function headings(): array
    {
        return [
            "用户编号",
            "用户昵称",
            "订单号",
            "微信单号",
            "退款单号",
            "商品名称",
            "规格",
            "数量",
            "商品原价",
            "支付金额",
            "订单创建时间",
            "支付时间",
            "是否是团购",
            "快递名称",
            "运单号",
            "支付状态",
            "发货状态",
            "收货状态",
            "收货人信息",
            "团购人员",
            "团购是否成功",
            "分享来源",
            "订单类型"
        ];
    }
}
